<?php

namespace App\Http\Controllers\admin;
use App\Model\my_order;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;


class OrderController extends Controller
{
    /**
    *	订单管理主页 视图 
    */
    public function getIndex(Request $request){
        // $a = my_order::orderBy('merstatus','desc') -> get();
        // 排除空值 
        $year = empty($request -> input('year')) ? date('Y',time()) : $request -> input('year');
        $month = empty($request -> input('month')) ? 1 : $request -> input('month');
        
        // 调整日期的时间戳 实现按时间搜索功能
        $timeBefore = strtotime($year.'/'.$month.'/'.'1');
        // 一个月的时间 精确获取每个月时间长度
        $timeAfter = empty($request -> input('month')) ? time() : $timeBefore+(60*60)*24*(date('t',$timeBefore));
       
    	// 所用订单查询
    	$data = DB::table('my_order') 
        -> whereBetween('ctime',[$timeBefore,$timeAfter])
    	-> orderBy('merstatus','desc')
    	-> orderBy('id','desc')
    	-> paginate($request -> input('count',$request -> input('count' , 10)));
        
        // 获取条数和金额
        $res = DB::table('my_order')
        -> whereBetween('ctime',[$timeBefore,$timeAfter])
        -> orderBy('merstatus','desc')
        -> orderBy('id','desc')
        -> select(DB::raw('count(*) as count,sum(price) as sum'))
        -> get()[0];
    	return view('admin.order.index',['data' => $data,'res' => $res,'request' => $request -> all()]);
    }
    /**
    *   订单表以及  订单详情表的数据删除
    */
    public function getOrderdel(Request $request){
        if(!$request -> has('id')){
            return 0;
        }
        // 对订单概览表进行删除
        $res = DB::table('my_order')
        -> where('id',$request -> input('id'))
        -> delete();
        
        // 对订单详情表进行删除 
        $res1 = DB::table('my_order_log')
        -> where('pid',$request -> input('id'))
        -> delete();

        if($res || $res1) {
            return 1;
        }else{
            return 0;
        }
    }
    /**
    *   订单详情视图
    *   param 订单id
    */
    public function getDetail(Request $request){
        if(!$request -> has('id')){
            return back() -> with('error','未知错误请刷新后重试');
        }
        // 订单详情表数据
        $data = DB::table('my_order_log')
        -> where('pid',$request -> input('id'))
        -> orderBy('status','desc')
        -> get();
        // 订单概览表数据
        $survey = DB::table('my_order')
        -> where('id',$request -> input('id'))
        -> select('goodsname','price')
        -> first();
     
        return view('admin.order.detail',['data' => $data,'survey' => $survey]);
    }
    /**
    *   订单详情对金额的操控
    */
    public function postMoney(Request $request) {
        if(!$request -> has('play')){
            return 0;
        }
        DB::beginTransaction();
        // 判断用户要执行的操作
        $res = 0;
        if($request -> input('play') == 1) {
            // 将金额转入买家账户
            $res1 = DB::update('update vault set money = money+'.$request -> input('money').' where uid = ?',[$request -> input('buyerid')]);
            $res2 = DB::update('update  vault set money = money-'.$request -> input('money').' where
                uid = ?',[$request -> input('sellerid')]);

            if($res1 && $res2) {
                DB::commit();
                $res = 1;
            } else {
                DB::rollback();
            }

            return $res;
        }

        if($request -> input('play') == 2) {
            // 将金额转入买家账户
            $res1 = DB::update('update vault set money = money-'.$request -> input('money').' where uid = ?',[$request -> input('buyerid')]);
            $res2 = DB::update('update  vault set money = money+'.$request -> input('money').' where
                uid = ?',[$request -> input('sellerid')]);

            if($res1 && $res2) {
                DB::commit();
                $res = 1;
            } else {
                DB::rollback();
            }

            return $res;
        }
    }
}
